using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using ZedGraph;

namespace ICP_MS.Plugin.iMass;

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripZedgraph : ToolStripControlHost
{
	private Thread threadRefresh = null;

	private const int LimitWidth = 375;

	public int RefreshTime { get; set; }

	public bool RefreshEnalbed { get; set; }

	public ZedGraphControl Zedgraph => base.Control as ZedGraphControl;

	public ToolStripZedgraph()
		: base(new ZedGraphControl())
	{
		RefreshTime = 250;
		RefreshEnalbed = true;
	}

	public void AddPoint(double x, double y)
	{
		if (base.Control is ZedGraphControl graph && graph.GraphPane.CurveList.Count == 1)
		{
			graph.GraphPane.CurveList[0].AddPoint(x, y);
		}
	}

	public void ClearPoints()
	{
		if (base.Control is ZedGraphControl graph && graph.GraphPane.CurveList.Count == 1)
		{
			graph.GraphPane.CurveList[0].Clear();
		}
	}

	public override Size GetPreferredSize(Size constrainingSize)
	{
		if (base.IsOnOverflow || base.Owner.Orientation == Orientation.Vertical)
		{
			return DefaultSize;
		}
		int width = base.Owner.DisplayRectangle.Width;
		if (base.Owner.OverflowButton.Visible)
		{
			width = width - base.Owner.OverflowButton.Width - base.Owner.OverflowButton.Margin.Horizontal;
		}
		int springBoxCount = 0;
		foreach (ToolStripItem item in base.Owner.Items)
		{
			if (!item.IsOnOverflow)
			{
				if (item is ToolStripZedgraph)
				{
					springBoxCount++;
					width -= item.Margin.Horizontal;
				}
				else
				{
					width = width - item.Width - item.Margin.Horizontal;
				}
			}
		}
		width -= 50;
		if (springBoxCount > 1)
		{
			width /= springBoxCount;
		}
		if (width < 375)
		{
			width = 375;
		}
		Size size = base.GetPreferredSize(constrainingSize);
		size.Width = width;
		return size;
	}

	protected override void OnSubscribeControlEvents(Control control)
	{
		base.OnSubscribeControlEvents(control);
		ZedGraphControl graph = base.Control as ZedGraphControl;
		InitZedgraph(graph);
	}

	protected override void OnUnsubscribeControlEvents(Control control)
	{
		base.OnUnsubscribeControlEvents(control);
	}

	public void RefreshGraph()
	{
		ZedGraphControl graph = base.Control as ZedGraphControl;
		GraphPane pane = graph.GraphPane;
		pane.AxisChange();
		graph.Invoke((MethodInvoker)delegate
		{
			graph.Refresh();
		});
	}

	private void InitZedgraph(ZedGraphControl graph)
	{
		GraphPane pane = graph.GraphPane;
		pane.Title.IsVisible = false;
		pane.Legend.IsVisible = false;
		pane.XAxis.Title.IsVisible = false;
		pane.YAxis.Title.IsVisible = false;
		pane.XAxis.Scale.IsVisible = false;
		pane.YAxis.Scale.IsVisible = false;
		pane.XAxis.Scale.Min = 0.0;
		pane.XAxis.Scale.Max = 267.0;
		pane.AddStick("", new PointPairList(), Color.Black);
		threadRefresh = new Thread((ThreadStart)delegate
		{
			while (!base.IsDisposed)
			{
				if (!base.DesignMode && RefreshEnalbed)
				{
					try
					{
						pane.AxisChange();
						graph.Invoke((MethodInvoker)delegate
						{
							graph.Refresh();
						});
					}
					catch
					{
					}
				}
				Thread.Sleep(RefreshTime);
			}
		});
		threadRefresh.Start();
	}

	private void DisposeZedgraph(ZedGraphControl graph)
	{
		if (threadRefresh != null && threadRefresh.IsAlive)
		{
			threadRefresh.Abort();
			threadRefresh = null;
		}
	}
}
